package com.zp.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.system.api.entity.SysOrgDeptGroup;
import com.zp.system.entity.SysOrgGroupVsDept;
import com.zp.system.entity.bo.SysOrgDeptGroupBo;
import com.zp.system.entity.vo.SysOrgDeptGroupVo;
import com.zp.system.mapper.SysOrgDeptGroupMapper;
import com.zp.system.mapper.SysOrgGroupVsDeptMapper;
import com.zp.system.service.ISysOrgDeptGroupService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 科室分组Service业务层处理
 *
 * @author zhang peng
 * @date 2022-12-18
 */
@RequiredArgsConstructor
@Service
public class SysOrgDeptGroupServiceImpl implements ISysOrgDeptGroupService {

    private final SysOrgDeptGroupMapper baseMapper;
    private final SysOrgGroupVsDeptMapper sysOrgGroupVsDeptMapper;

    /**
     * 查询科室分组
     */
    @Override
    public SysOrgDeptGroupVo queryById (String deptGroupId) {
        SysOrgDeptGroupVo vo = baseMapper.selectVoById(deptGroupId);
        if (ObjectUtil.isNotNull(vo)) {
            List<SysOrgGroupVsDept> orgGroupVsDeptList = sysOrgGroupVsDeptMapper.selectList(Wrappers.<SysOrgGroupVsDept>lambdaQuery()
                    .eq(SysOrgGroupVsDept::getDeptGroupId, deptGroupId));
            String[] deptIds = orgGroupVsDeptList.stream().map(SysOrgGroupVsDept::getDeptId).toArray(String[]::new);
            vo.setDeptIds(deptIds);
            return vo;
        } else {
            throw new RuntimeException("修改的分组不存在，请刷新页面");
        }

    }

    /**
     * 查询科室分组列表(分页)
     */
    @Override
    public TableDataInfo<SysOrgDeptGroupVo> queryPageList (SysOrgDeptGroupBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysOrgDeptGroup> lqw = buildQueryWrapper(bo);
        Page<SysOrgDeptGroupVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询科室分组列表(不分页)
     */
    @Override
    public List<SysOrgDeptGroupVo> queryList (SysOrgDeptGroupBo bo) {
        LambdaQueryWrapper<SysOrgDeptGroup> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysOrgDeptGroup> buildQueryWrapper (SysOrgDeptGroupBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysOrgDeptGroup> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getDeptGroupName()), SysOrgDeptGroup::getDeptGroupName, bo.getDeptGroupName());
        lqw.eq(StringUtils.isNotBlank(bo.getOrgId()), SysOrgDeptGroup::getOrgId, bo.getOrgId());
        lqw.eq(bo.getOrderNum() != null, SysOrgDeptGroup::getOrderNum, bo.getOrderNum());
        return lqw;
    }

    /**
     * 新增科室分组
     */
    @Override
    public Boolean insertByBo (SysOrgDeptGroupBo bo) {
        SysOrgDeptGroup add = BeanUtil.toBean(bo, SysOrgDeptGroup.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            saveGroupVsDept(add);
            bo.setDeptGroupId(add.getDeptGroupId());
        }
        return flag;
    }

    /**
     * 保存对照
     */
    public void saveGroupVsDept (SysOrgDeptGroup deptGroup) {
        List<SysOrgGroupVsDept> list = new ArrayList<>();
        sysOrgGroupVsDeptMapper.delete(Wrappers.<SysOrgGroupVsDept>lambdaQuery()
                .eq(SysOrgGroupVsDept::getDeptGroupId, deptGroup.getDeptGroupId()));
        for (String deptId : deptGroup.getDeptIds()) {
            SysOrgGroupVsDept sysOrgGroupVsDept = new SysOrgGroupVsDept();
            sysOrgGroupVsDept.setDeptGroupId(deptGroup.getDeptGroupId());
            sysOrgGroupVsDept.setDeptId(deptId);
            list.add(sysOrgGroupVsDept);
        }
        sysOrgGroupVsDeptMapper.insertBatch(list);
    }

    /**
     * 修改科室分组
     */
    @Override
    public Boolean updateByBo (SysOrgDeptGroupBo bo) {
        SysOrgDeptGroup update = BeanUtil.toBean(bo, SysOrgDeptGroup.class);
        validEntityBeforeSave(update);
        saveGroupVsDept(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave (SysOrgDeptGroup entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除科室分组
     */
    @Override
    public Boolean deleteWithValidByIds (Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        for (String id : ids) {
            sysOrgGroupVsDeptMapper.delete(Wrappers.<SysOrgGroupVsDept>lambdaQuery()
                    .eq(SysOrgGroupVsDept::getDeptGroupId, id));
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
